package com.suxin.module.sender.producer;

import com.suxin.module.bean.MyPoint;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Classname Producer
 * @Description [ TODO ]
 * @Author Tang
 * @Date 2020/10/15 15:08
 * @Created by ASUS
 */
@Slf4j
public class MyProducerImpl implements MyProducer{

    private BlockingQueue<MyPoint> blockingQueue;

    private final AtomicBoolean forbiddenWrite = new AtomicBoolean(false);

    public MyProducerImpl(BlockingQueue<MyPoint> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void send(MyPoint myPoint) {
        if (forbiddenWrite.get()) {
            throw new IllegalStateException("client has been closed.");
        }
        try {
            blockingQueue.put(myPoint);
        } catch (InterruptedException e) {
            log.error("Client Thread been Interrupted.", e);
            e.printStackTrace();
        }
    }

    @Override
    public void forbiddenSend() {
        forbiddenWrite.compareAndSet(false, true);
    }
}